home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / borland / bgiherc.zip / BGIDEMO.C next >
Text File  |  1989-05-31  |  40KB  |  1,402 lines

  1. /*
  2.    GRAPHICS DEMO FOR TURBO C 2.0
  3.  
  4.    Copyright (c) 1987,88 Borland International. All rights reserved.
  5.  
  6.    Altered (modestly) to support the Hercules InColor Card using the
  7.    "unified" HERC.BGI driver.
  8.  
  9. */
  10.  
  11. #ifdef __TINY__
  12. #error BGIDEMO will not run in the tiny model.
  13. #endif
  14.  
  15. #include <dos.h>
  16. #include <math.h>
  17. #include <conio.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21.  
  22. #include <graphics.h>
  23.  
  24. #define ESC    0x1b            /* Define the escape key    */
  25. #define TRUE    1            /* Define some handy constants    */
  26. #define FALSE    0            /* Define some handy constants    */
  27. #define PI    3.14159         /* Define a value for PI    */
  28. #define ON    1            /* Define some handy constants    */
  29. #define OFF    0            /* Define some handy constants    */
  30.  
  31. char *Fonts[] = {
  32.   "DefaultFont",   "TriplexFont",   "SmallFont",
  33.   "SansSerifFont", "GothicFont"
  34. };
  35.  
  36. char *LineStyles[] = {
  37.   "SolidLn",  "DottedLn",  "CenterLn",  "DashedLn",  "UserBitLn"
  38. };
  39.  
  40. char *FillStyles[] = {
  41.   "EmptyFill",  "SolidFill",      "LineFill",      "LtSlashFill",
  42.   "SlashFill",  "BkSlashFill",    "LtBkSlashFill", "HatchFill",
  43.   "XHatchFill", "InterleaveFill", "WideDotFill",   "CloseDotFill"
  44. };
  45.  
  46. char *TextDirect[] = {
  47.   "HorizDir",  "VertDir"
  48. };
  49.  
  50. char *HorizJust[] = {
  51.   "LeftText",   "CenterText",   "RightText"
  52. };
  53.  
  54. char *VertJust[] = {
  55.   "BottomText",  "CenterText",  "TopText"
  56. };
  57.  
  58. struct PTS {
  59.   int x, y;
  60. };    /* Structure to hold vertex points    */
  61.  
  62. int    GraphDriver;        /* The Graphics device driver        */
  63. int    GraphMode;        /* The Graphics mode value        */
  64. double AspectRatio;        /* Aspect ratio of a pixel on the screen*/
  65. int    MaxX, MaxY;        /* The maximum resolution of the screen */
  66. int    MaxColors;        /* The maximum # of colors available    */
  67. int    ErrorCode;        /* Reports any graphics errors        */
  68. struct palettetype palette;    /* Used to read palette info    */
  69.  
  70. /*                                    */
  71. /*    Function prototypes                        */
  72. /*                                    */
  73.  
  74. void Initialize(void);
  75. void ReportStatus(void);
  76. void TextDump(void);
  77. void Bar3DDemo(void);
  78. void RandomBars(void);
  79. void TextDemo(void);
  80. void ColorDemo(void);
  81. void ArcDemo(void);
  82. void CircleDemo(void);
  83. void PieDemo(void);
  84. void BarDemo(void);
  85. void LineRelDemo(void);
  86. void PutPixelDemo(void);
  87. void PutImageDemo(void);
  88. void LineToDemo(void);
  89. void LineStyleDemo(void);
  90. void CRTModeDemo(void);
  91. void UserLineStyleDemo(void);
  92. void FillStyleDemo(void);
  93. void FillPatternDemo(void);
  94. void PaletteDemo(void);
  95. void PolyDemo(void);
  96. void SayGoodbye(void);
  97. void Pause(void);
  98. void MainWindow(char *header);
  99. void StatusLine(char *msg);
  100. void DrawBorder(void);
  101. void changetextstyle(int font, int direction, int charsize);
  102. int  gprintf(int *xloc, int *yloc, char *fmt, ... );
  103.  
  104. /*                                    */
  105. /*    Begin main function                        */
  106. /*                                    */
  107.  
  108.  
  109. int main()
  110. {
  111.   Initialize();         /* Set system into Graphics mode    */
  112.   ReportStatus();        /* Report results of the initialization */
  113.  
  114.   ColorDemo();            /* Begin actual demonstration        */
  115.   if( GraphDriver==EGA || GraphDriver==EGALO || GraphDriver==VGA )
  116.     PaletteDemo();
  117.   if( GraphDriver==HERCMONO && (getmaxcolor() == 15)) /* Test for InColor */
  118.     PaletteDemo();
  119.   PutPixelDemo();
  120.   PutImageDemo();
  121.   Bar3DDemo();
  122.   BarDemo();
  123.   RandomBars();
  124.   ArcDemo();
  125.   CircleDemo();
  126.   PieDemo();
  127.   LineRelDemo();
  128.   LineToDemo();
  129.   LineStyleDemo();
  130.   UserLineStyleDemo();
  131.   TextDump();
  132.   TextDemo();
  133.   CRTModeDemo();
  134.   FillStyleDemo();
  135.   FillPatternDemo();
  136.   PolyDemo();
  137.   SayGoodbye();         /* Give user the closing screen     */
  138.  
  139.   closegraph();         /* Return the system to text mode    */
  140.   return(0);
  141. }
  142.  
  143. /*                                    */
  144. /*    INITIALIZE: Initializes the graphics system and reports     */
  145. /*    any errors which occured.                    */
  146. /*                                    */
  147.  
  148. void Initialize(void)
  149. {
  150.   int xasp, yasp;            /* Used to read the aspect ratio*/
  151.  
  152.   GraphDriver = DETECT;
  153.   initgraph( &GraphDriver, &GraphMode, "" );
  154.   ErrorCode = graphresult();        /* Read result of initialization*/
  155.   if( ErrorCode != grOk ){        /* Error occured during init    */
  156.     printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
  157.     exit( 1 );
  158.   }
  159.  
  160.   getpalette( &palette );        /* Read the palette from board    */
  161.   MaxColors = getmaxcolor() + 1;    /* Read maximum number of colors*/
  162.  
  163.   MaxX = getmaxx();
  164.   MaxY = getmaxy();            /* Read size of screen        */
  165.  
  166.   getaspectratio( &xasp, &yasp );    /* read the hardware aspect    */
  167.   AspectRatio = (double)xasp / (double)yasp; /* Get correction factor    */
  168.  
  169. }
  170.  
  171. /*                                    */
  172. /*    REPORTSTATUS: Report the current configuration of the system    */
  173. /*    after the auto-detect initialization.                */
  174. /*                                    */
  175.  
  176. void ReportStatus(void)
  177. {
  178.   struct viewporttype      viewinfo;    /* Params for inquiry procedures*/
  179.   struct linesettingstype lineinfo;
  180.   struct fillsettingstype fillinfo;
  181.   struct textsettingstype textinfo;
  182.   struct palettetype      palette;
  183.  
  184.   char *driver, *mode;            /* Strings for driver and mode    */
  185.   int x, y;
  186.  
  187.   getviewsettings( &viewinfo );
  188.   getlinesettings( &lineinfo );
  189.   getfillsettings( &fillinfo );
  190.   gettextsettings( &textinfo );
  191.   getpalette( &palette );
  192.  
  193.   x = 10;
  194.   y = 4;
  195.  
  196.   MainWindow( "Status report after InitGraph" );
  197.   settextjustify( LEFT_TEXT, TOP_TEXT );
  198.  
  199.   driver = getdrivername();
  200.   mode = getmodename(GraphMode);    /* get current setting        */
  201.  
  202.   gprintf( &x, &y, "Graphics device    : %-20s (%d)", driver, GraphDriver );
  203.   gprintf( &x, &y, "Graphics mode      : %-20s (%d)", mode, GraphMode );
  204.   gprintf( &x, &y, "Screen resolution  : ( 0, 0, %d, %d )", getmaxx(), getmaxy() );
  205.  
  206.   gprintf( &x, &y, "Current view port  : ( %d, %d, %d, %d )",
  207.   viewinfo.left, viewinfo.top, viewinfo.right, viewinfo.bottom );
  208.   gprintf( &x, &y, "Clipping           : %s", viewinfo.clip ? "ON" : "OFF" );
  209.  
  210.   gprintf( &x, &y, "Current position   : ( %d, %d )", getx(), gety() );
  211.   gprintf( &x, &y, "Colors available   : %d", MaxColors );
  212.   gprintf( &x, &y, "Current color      : %d", getcolor() );
  213.  
  214.   gprintf( &x, &y, "Line style         : %s", LineStyles[ lineinfo.linestyle ] );
  215.   gprintf( &x, &y, "Line thickness     : %d", lineinfo.thickness );
  216.  
  217.   gprintf( &x, &y, "Current fill style : %s", FillStyles[ fillinfo.pattern ] );
  218.   gprintf( &x, &y, "Current fill color : %d", fillinfo.color );
  219.  
  220.   gprintf( &x, &y, "Current font       : %s", Fonts[ textinfo.font ] );
  221.   gprintf( &x, &y, "Text direction     : %s", TextDirect[ textinfo.direction ] );
  222.   gprintf( &x, &y, "Character size     : %d", textinfo.charsize );
  223.   gprintf( &x, &y, "Horizontal justify : %s", HorizJust[ textinfo.horiz ] );
  224.   gprintf( &x, &y, "Vertical justify   : %s", VertJust[ textinfo.vert ] );
  225.  
  226.   Pause();                /* Pause for user to read screen*/
  227.  
  228. }
  229.  
  230. /*                                    */
  231. /*    TEXTDUMP: Display the all the characters in each of the     */
  232. /*    available fonts.                        */
  233. /*                                    */
  234.  
  235. void TextDump()
  236. {
  237.   static int CGASizes[]  = {
  238.     1, 3, 7, 3, 3   };
  239.   static int NormSizes[] = {
  240.     1, 4, 7, 4, 4   };
  241.  
  242.   char buffer[80];
  243.   int font, ch, wwidth, lwidth, size;
  244.   struct viewporttype vp;
  245.  
  246.   for( font=0 ; font<5 ; ++font ){    /* For each available font    */
  247.     sprintf( buffer, "%s Character Set", Fonts[font] );
  248.     MainWindow( buffer );        /* Display fontname as banner    */
  249.     getviewsettings( &vp );        /* read current viewport    */
  250.  
  251.     settextjustify( LEFT_TEXT, TOP_TEXT );
  252.     moveto( 2, 3 );
  253.  
  254.     buffer[1] = '\0';                   /* Terminate string             */
  255.     wwidth = vp.right - vp.left;    /* Determine the window width    */
  256.     lwidth = textwidth( "H" );          /* Get average letter width     */
  257.  
  258.     if( font == DEFAULT_FONT ){
  259.       changetextstyle( font, HORIZ_DIR, 1 );
  260.       ch = 0;
  261.       while( ch < 256 ){        /* For each possible character    */
  262.     buffer[0] = ch;         /* Put character into a string    */
  263.     outtext( buffer );        /* send string to screen    */
  264.     if( (getx() + lwidth) > wwidth )
  265.       moveto( 2, gety() + textheight("H") + 3 );
  266.     ++ch;                /* Goto the next character    */
  267.       }
  268.     }
  269.     else{
  270.  
  271.       size = (MaxY < 200) ? CGASizes[font] : NormSizes[font];
  272.       changetextstyle( font, HORIZ_DIR, size );
  273.  
  274.       ch = '!';                         /* Begin at 1st printable       */
  275.       while( ch < 127 ){        /*